package mysgoog.util;

import java.io.*;
import java.util.*;

import mysgoog.gtp.Vertex;
/**
* @author Fu, Zhongyang
*/
public class EmptyDBReader
{
    List<String> pattern = new ArrayList<String>();
    String s = "";
    String ans = "";
    String ansud = "";
    String anslr = "";
    String ansudlr = "";
    List<Vertex> list = new ArrayList<Vertex>();;
    int l = 0;
    int w = 0;
    int x = 0;
    int y = 0;
    int count = 0;

    public static void main(String arg[])
    {
        new EmptyDBReader("pattern\\EmptyPattern.db");
    }

    public EmptyDBReader(String filename)
    {
        read(filename);
        FileManager.write("pattern\\EmptyPattern.dbpattern", s);
    }

    public void read(String filename)
    {
        BufferedReader reader = null;
        try
        {
            File file = new File(filename);
            reader = new BufferedReader(new FileReader(file));
            String text = null;
            while ((text = reader.readLine()) != null)
            {
                //System.err.println(text);
            	//System.out.println(count++);
                parse(text);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public void parse(String content){
    	if (content.length() == 0) return;
    	String[] token = content.split(" ");
    	switch (token[0].charAt(0)){
    		case '#':
    			return;
    		case ':':
    			s += patternSize(0) + " " + ans + " " + parseValue(content, 0) + parseList(0) + "\n";
    			s += patternSize(1) + " " + ansud + " " + parseValue(content, 1) + parseList(1) + "\n";
    			s += patternSize(2) + " " + anslr + " " + parseValue(content, 2) + parseList(2) + "\n";
    			s += patternSize(3) + " " + ansudlr + " " + parseValue(content, 3) + parseList(3) + "\n";
    			return;
    		case ';':
    			//
    			return;
    		case 'P':
    			ans = "";
    			ansud = "";
    			anslr = "";
    			ansudlr = "";
    			l = 0;
    			w = 0;
    			list = new ArrayList<Vertex>();
    			return;
    		case '>':
    			return;
    		default : 
    			parsePattern(content);
    			return;
    	}
    	
    }    
 
    public String parseValue(String content, int i){    	
    	content = (String) content.substring(1, content.length());
    	//System.out.println(content);
    	String ans = new String();
    	String token[] = content.split(" ");
    	int x = Integer.parseInt(token[0]);
    	int y = Integer.parseInt(token[1]);
    	ans = token[2];
    	ans += " " + token[3];
    	switch (i){
    		case 0:
   				ans += " " + x + " " + y;
    			return ans;
    		case 1:
   				ans += " " + (l - 1 - x) + " " + y;
    			return ans;    			
    		case 2:
    				ans += " " + x + " " + (w - 1 - y);
    			return ans;    			
    		case 3:
   				ans += " " + (l - 1 - x) + " " + (w - 1 - y);
    			return ans;    			
    	}
    	return null;
    }
    
    public int symx(int x, int i){    	
    	int ans;
    	switch (i){
    		case 0:
   				ans = x;
   				return ans;
    		case 1:
   				ans = (l - 1 - x);
    			return ans;    			
    		case 2:
    			ans = x;
    			return ans;    			
    		case 3:
   				ans = (l - 1 - x);
    			return ans;    			
    	}
    	return -1;
    }
    
    public int symy(int y, int i){    	
    	int ans;
    	switch (i){
    		case 0:
   				ans = y;
   				return ans;
    		case 1:
   				ans = y;
    			return ans;    			
    		case 2:
    			ans = (w - 1 - y);
    			return ans;    			
    		case 3:
   				ans = (w - 1 - y);
    			return ans;    			
    	}
    	return -1;
    }
    
    public String parseList(int i){
    	//System.out.println("l " + l + " " + "w " + w + " i " + i);
    	String ans = new String();
    	for (Vertex v:list){
    		ans += " " + symx(v.x, i) + " " + symy(v.y, i);
    		//System.out.println(v.x + " " + v.y);
    		//System.out.println(ans);
    	}

    	return ans;    	
    }
    
    public void parsePattern(String content){    	
    	content = content.replace('-', '|');
    	content = content.replace('+', '|');    	
    	if (content.contains("*")) {
    		x = l;
    		for (int i = 0; i < content.length(); i++){
    			if (content.charAt(i) == '*') {
    				y = i;    				
    				break;
    			}
    		}
    		content = content.replace('*', '.');
    	}
    	for (int i = 0; i < content.length(); i++){
    		if (content.charAt(i) == '.') list.add(new Vertex(l, i));
    	}
    	w = content.length();
    	l++;
    	ans += content;
    	ansud = content + ansud;
    	StringBuilder builder = new StringBuilder(content);
    	builder.reverse();
    	anslr += builder;
    	ansudlr = builder + ansudlr;
    }
    
    public String patternSize(int i){
    	switch (i){
    		case 0:
    			return l + " " + w;
    		case 1:
    			return l + " " + w; 
    		case 2:
    			return l + " " + w;
    		case 3:
    			return l + " " + w;
    	}
    	return null;
    }
    
}
